package com.lg.algorithm.ms;

import java.util.Arrays;

public class Boss {
    public static void main(String[] args) {

        int[] a = {1, 2, 2, 3, 4};
        int[] b = {2, 4, 5};
        char[] chars1 = {'a','b','c','d','e','f','g'};
        char[] chars2 = {'a','b','c','d','f','g'};
        char[] chars3 = {'a','b','c','d','f','g','v'};

        System.out.println(Arrays.toString(retainAll(a, b)));
        System.out.println(find(chars1,chars2));
        System.out.println(find(chars3,chars2));
    }

    public static char find(char[] chars1, char[] chars2) {
        int index = 0;
        while (true) {
            if (chars1[index] == chars2[index]) {
                index++;
                //第一个比第二个长；当chars1多出的字符在最后
                if (index == chars2.length) {
                    break;
                }
            } else {
                return chars1[index];
            }
        }
        return chars1[index];
    }
    public static int[] retainAll(int[] array1, int[] arry2) {
        int index1 = 0;
        int index2 = 0;
        StringBuilder resStr = new StringBuilder();

        while (true) {
            if (index1 == array1.length || index2 == arry2.length) {
                break;
            }
            if (array1[index1] == arry2[index2]) {
                resStr.append(array1[index1] + "::");
                index1++;
                index2++;
            } else if (array1[index1] > arry2[index2]) {
                index2++;
            } else {
                index1++;
            }
        }
        String[] resStrArr = resStr.toString().split("::");

        //strArr  to integerArr

        int[] res = Arrays.stream(resStrArr).mapToInt(Integer::parseInt).toArray();
        return res;
    }


}


